<template>
  <div class="container">
    <el-row>
      <el-col :span="8">
        <el-card>
          <div>
            <el-button size="mini" type="warning" plain @click="add(0, {})"
              >新增一级权限</el-button
            >
          </div>
          <div class="authTree">
            <div class="item">
              <el-tree
                :data="power.auth"
                @node-click="check"
                default-expand-all
                :expand-on-click-node="false"
                node-key="name"
                ref="authTree"
              >
                <span class="custom-tree-node" slot-scope="{ node, data }">
                  <span class="label">{{ node.label }}</span>
                  <span v-if="node.label === curNode.label">
                    <el-button
                      class="add"
                      type="text"
                      size="mini"
                      @click.stop="add(0, data)"
                      >新增</el-button
                    >
                    <el-button
                      type="text"
                      size="mini"
                      @click.stop="add(1, data)"
                      >编辑</el-button
                    >
                    <el-button
                      class="del"
                      type="text"
                      size="mini"
                      @click.stop="remove(node, data)"
                      >删除</el-button
                    >
                  </span>
                </span>
              </el-tree>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
    <el-dialog
      :visible.sync="editShow"
      width="450px"
      :title="editType == 1 ? '编辑权限' : '新增权限'"
    >
      <el-form label-width="80px" ref="form" :model="form" :rules="rules">
        <el-form-item label="角色名称" prop="label">
          <el-input v-model="form.label"></el-input>
        </el-form-item>
        <el-form-item label="唯一标识" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button class="confirm" type="primary" size="small" @click="send"
            >提交</el-button
          >
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters, mapState } from "vuex";
import {
  getAuthTree_role,
  AddTopAuthList_role,
  DeleteAuthList_role,
  EditAuthList_role,
} from "@/api/role.js";
export default {
  name: "authManage",
  data() {
    return {
      editType: 1,
      editShow: false,
      form: {
        label: "",
        name: "",
        children: [],
      },
      rules: {
        label: [{ required: true, message: "请输入权限名称", trigger: "blur" }],
        name: [{ required: true, message: "请输入权限标识", trigger: "blur" }],
      },
      curNode: {},
      id: "",
      level: "",
      type: 0,
    };
  },
  created() {
    this.getAuthTree_role(); //获取tree列表
  },
  computed: {
    ...mapState(["power"]),
  },
  methods: {
    getAuthTree_role() {
      getAuthTree_role().then((res) => {
        // console.log(res);
        this.$store.commit("getRoleTreeList_power", res.data.list);
      });
    },
    check(data, node) {
      this.curNode = data;
    },
    add(type, data) {
      // console.log(type, data.id);
      // console.log(type, data.level);
      this.id = data.id;
      this.level = data.level;

      this.type = type;
      // this.curNode = data
      this.editShow = true;
      this.editType = type;
      if (type == 1) {
        this.form = { ...data };
      } else {
        this.form = {
          label: "",
          name: "",
          children: [],
        };
      }
    },
    remove(node, data) {
      console.log(node,'node');
      console.log(data,'data');
      this.$confirm("确认操作吗？", {
        type: "warning",
      })
        .then(() => {
          DeleteAuthList_role({
            name: data.name,
            level: data.level,
          }).then((res) => {
            if (res.status == 0) {
              this.$message({
                type: "success",
                message: res.msg,
              });
              this.editShow = false;
              this.getAuthTree_role(); 
            } else {
              this.$message({
                type: "error",
                message: res.msg,
              });
            }
          });
        })
        .catch(() => {});
    },
    send() {
      if (this.type) {
        EditAuthList_role({
          name: this.form.name,
          label: this.form.label,
          id: this.id,
        }).then((res) => {
          console.log(res);
          location.reload();
        });
      } else {
        this.AddTopAuthList_role(
          this.form.name,
          this.form.label,
          this.id,
          this.level
        );
      }
      this.editType = false;
    },
    AddTopAuthList_role(name, label, id = "", level = "") {
      AddTopAuthList_role({
        name,
        label,
        id,
        level,
      }).then((res) => {
        if (res.status == 0) {
          this.$message({
            type: "success",
            message: res.msg,
          });
          this.editShow = false;
          this.getAuthTree_role(); //获取tree列表
        } else {
          this.$message({
            type: "error",
            message: res.msg,
          });
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.control {
  display: flex;
  justify-content: flex-end;
}
.authTree {
  display: flex;
  padding: 20px;
  .item {
    flex: 1;
  }
  .custom-tree-node {
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .label {
      font-size: 14px;
    }
    .add {
      color: #e6a23c;
    }
    .del {
      color: #f56c6c;
    }
  }
}
</style>
